package com.cw.maintenance.action;

import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JOptionPane;

import com.cw.maintenance.MaintenancePanel;

public class LoadAction extends AbstractAction {
	BufferedReader br = null;
	MaintenancePanel mpanel;

	public LoadAction(String name, Icon icon, MaintenancePanel mpanel) {
		super(name, icon);
		this.mpanel = mpanel;
	}

	/**
	 * Invoked when an action occurs.
	 * 
	 * @param e
	 *            ActionEvent
	 * @todo Implement this java.awt.event.ActionListener method
	 */
	public void actionPerformed(ActionEvent e) {

		// br = new BufferedReader(
		// new InputStreamReader(new FileInputStream(
		// SystemProperties.USER_DIR
		// + mpanel.getOper().getSaveFile())));
//		try {
//			br = new BufferedReader(new InputStreamReader(LoadAction.class
//					.getResource(mpanel.getOper().getSaveFile()).openStream()));
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		if (br != null) {
			int comfirm = JOptionPane
					.showInternalConfirmDialog(
							mpanel,
							"You must remove all record first,then load this from file?",
							"Confirm", JOptionPane.YES_NO_OPTION);
			if (comfirm == JOptionPane.NO_OPTION) {
				return;
			}
//			mpanel.removeAllRec();
		}

		new Thread(new Runnable() {
			public void run() {
				String data = null;
				try {
					while ((data = br.readLine()) != null) {
						Vector rec = new Vector();
						data = data.substring(data.indexOf('[') + 1, data
								.indexOf(']'));
						String[] splitStr = data.split(",");
						for (int i = 0; i < splitStr.length; i++) {
							rec.add(splitStr[i].trim());
						}
						// System.out.println(data);
						mpanel.insertRec(rec);
					}
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}).start();

	}
}
